package com.hjx.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hjx.entity.SysMenu;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysMenuService;
import com.hjx.util.MenusUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/17 - 10:06
 */
@RestController
@RequestMapping("menu")
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;
    @Resource
    MenusUtil menusUtil;

    @LogAnnotation
    @GetMapping("/findOne/{mid}")
    public AjaxResponse findOne(@PathVariable String mid) {
        final SysMenu menu = sysMenuService.findOne(mid);
        if (menu != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, menu);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    @LogAnnotation
    @GetMapping("/findAll")
    public AjaxResponse findAll() {
        List<SysMenu> menus = sysMenuService.findAll();
        return new AjaxResponse(ResponseType.QUERY_SUCCESS, menus);

    }

    @LogAnnotation
    @GetMapping("/findAllPage/{pageNum}/{pageSize}")
    public AjaxResponse findAll(@PathVariable(name = "pageNum") Integer pageNum,
                                @PathVariable(name = "pageSize") Integer pageSize) {
        Page<SysMenu> page = PageHelper.startPage(pageNum, pageSize, true);
        sysMenuService.findAll();
        return new AjaxResponse(ResponseType.QUERY_SUCCESS, page.toPageInfo());

    }


    @LogAnnotation
    @PostMapping("/insert")
    public AjaxResponse insert(@RequestBody SysMenu sysMenu) {
        final boolean b = sysMenuService.insert(sysMenu);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @PostMapping("/insertBatch")
    public AjaxResponse insertBatch(@RequestBody SysMenu[] menus) {
        final boolean b = sysMenuService.insertBatch(menus);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("/delete/{mid}")
    public AjaxResponse delete(@PathVariable String mid) {
        final boolean b = sysMenuService.delete(mid);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @PostMapping("/update")
    public AjaxResponse update(@RequestBody SysMenu sysMenu) {
        final boolean b = sysMenuService.update(sysMenu);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("/findMenuAndChildren/{parentId}")
    public AjaxResponse findMenuAndChildren(@PathVariable String parentId) {
        final List<SysMenu> fatherMenus = sysMenuService.findByParentId(parentId);
        if (fatherMenus != null) {
            for (SysMenu menu : fatherMenus) {
                menusUtil.getMenuAndChildren(menu);
            }
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, fatherMenus);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }
}
